/*-----------------------------------------------------
 * 权限子系统-系统消息模块
 * ---------------------------------------------------*/
jx.ready(function () {
    //定义变量
    var api = {
        grid: '/auth/message/grid',
        create: '/auth/message/create',
        edit: '/auth/message/edit',
        delete: '/auth/message/delete',
        details: '/auth/message/details',
        export: '/auth/message/export'
    };
    var $grid = $('#grid'), $gridform = $('#gridform'),
        $timeButton = $('#time-horizon > a'),
        $griddatePopover = $('#griddate-popover'), $gridPanel;
    var gridInstance;
    var dialogWidth = $grid.data('dialogWidth'), dialogHeight = $grid.data('dialogHeight');
    var createTitle = '发送系统消息', editTitle = '修改系统消息', detailsTitle = '查看系统消息';
    var gridUrl = jx.apiUrl(api.grid), createUrl = jx.apiUrl(api.create),
        editUrl = jx.apiUrl(api.edit), deleteUrl = jx.apiUrl(api.delete),
        detailsUrl = jx.apiUrl(api.details), exportUrl = jx.apiUrl(api.export);

    var formSubmit = function () {
        $gridform.find(':submit').click();
    }

    var setTimeHorizon = function ($this) {
        $timeButton.removeClass('active');
        $this.addClass('active');
        var val = $this.attr('data-value');
        if (val == '5') return;
        var $start = $('#operateStartDateTime');
        var $end = $('#operateEndDateTime');
        var cdate = jx.formatDate(new Date());
        switch (val) {
            case '1'://今天
                $start.val(cdate);
                $end.val(cdate);
                formSubmit();
                break;
            case '2'://近7天
                $start.val(jx.formatDate(jx.addDate(new Date(), -7)));
                $end.val(cdate);
                formSubmit();
                break;
            case '3'://近一个月
                $start.val(jx.formatDate(jx.addDate(new Date(), -30)));
                $end.val(cdate);
                formSubmit();
                break;
            case '4'://近三个月
                $start.val(jx.formatDate(jx.addDate(new Date(), -90)));
                $end.val(cdate);
                formSubmit();
                break;
            case '5'://自定义
                break;
        }
    };

    //初始化表格
    var initGrid = function () {
        gridInstance = $grid.jxgrid({
            url: gridUrl,
            onDblClickRow: function (index, row) {
                detailsData(row);
            },
            onBeforeLoad: function (param) {
                if (param.operateEndDateTime && param.operateStartDateTime) {
                    return true;
                }
                return false;
            },
            onRowContextMenu: function (e, index, row) {
                jx.auth.gridContextMenuHandle(gridInstance, e, index, row)
            },
            onLoadSuccess: function () {
                jx.auth.showGridContextMenu(gridInstance, '#gridcmenu');
            }
        });
        $('#btn-datesearch').click(function () {
            if ($('#griddateform').valid()) {
                formSubmit();
                $griddatePopover.popoverX('hide');
            }
        });
        $timeButton.click(function () {
            setTimeHorizon($(this));
        });
    };

    //初始化事件
    var initEvent = function () {
        $('#btn-create').click(function () {
            createData();
        });
        $('#btn-edit,#btn-cmedit').click(function () {
            editData();
        });
        $('#btn-delete,#btn-cmdelete').click(function () {
            deleteData();
        });
        $('#btn-export').click(function () {
            exportData();
        });
    };

    //新建数据
    var createData = function () {
        jx.dialog({
            title: createTitle,
            url: createUrl,
            width: dialogWidth,
            height: dialogHeight
        });
    };

    //编辑数据
    var editData = function () {
        jx.auth.editData(gridInstance, {
            title: editTitle,
            url: editUrl,
            width: dialogWidth,
            height: dialogHeight
        });
    };

    //删除数据
    var deleteData = function () {
        jx.auth.deleteData(gridInstance, {
            url: deleteUrl
        });
    };

    //查看数据
    var detailsData = function (row) {
        var receiveUserId = jx.getUrlParam('receiveUserId');
        jx.auth.detailsData(gridInstance, row, {
            title: detailsTitle,
            url: detailsUrl,
            params: {receiveUserId: receiveUserId},
            maxmin: true,
            offset: 'auto',
            width: '70%',
            height:'80%'
        });
    };

    //导出数据
    var exportData = function () {
        jx.auth.exportData(exportUrl, jx.serialize($gridform));
    };

    //定义表格列格式化函数
    jx.gf.status = function (v, row, index) {
        var cls;
        var label;
        if ((jx.isNumber(v) && v === 1) || (jx.isBool(v) && v === true)) {
            cls = 'success';
            label = '已读';
        }
        else {
            cls = 'danger';
            label = '未读';
        }
        return '<span class="label label-' + cls + '">' + label + '</span>';
    };

    //对外接口 重新加载表格数据
    window.reloadGridData = function () {
        gridInstance.reloadGridData();
    };

    //初始化
    initGrid();
    initEvent();
    $timeButton.eq(3).click();
});